#include "AppDelegate.h"
#include "UISetting.h"
#include "SceneMainMenu.h"
#include <Windows.h>

UISetting * UISetting::create(unsigned int eventflag)
{
	auto uist = new UISetting();
	uist->initWithColor(THEMECOLORBA(230));
	uist->setEventFlag(eventflag);
	SettingOptionEventFlag = eventflag;
	uist->createTitle();
	uist->createTab("Common", "image/MainMenu/SettingCommon.png");
	uist->createTab("Key Binding", "image/MainMenu/SettingKeybinding.png");
	uist->createPanel(frameSize.height*(.8f), 2, 2, 8);
	return uist;
}

void UISetting::close()
{
	UI::moveEventFlagDown();
	runAction(
		Sequence::create(
			EaseQuarticActionOut::create(MoveTo::create(.2f, Vec2(0, -getContentSize().height))),
			RemoveSelf::create(),
			NULL
		)
	);
}

void UISetting::onTabClick()
{
	for (auto tab : tabs)
	{
		if (tab->mouseStatus == MOUSEENTER || tab->mouseStatus == MOUSEHOVER)
		{
			tab->setActiveStat(true);
			panel->runAction(
				EaseBackOut::create(
					MoveTo::create(.33f, Vec2(-frameSize.width*tab->getTag(), 0))
				)
			);
		}
		else tab->setActiveStat(false);
	}
}

void UISetting::createTitle()
{
	auto t = Label::createWithTTF("GAME SETTING", FONTA, 64);
	t->setTextColor(TEXTCOLORB);
	UI::setNodeAnch(t, ANCH_TL);
	t->setPosition(frameSize.width*.05f, frameSize.height*.95f);
	addChild(t);

	string hint = "All options are autosave.\n";
	hint += "Options marked with ""*"" are able to effect after restarted.\n";
	hint += "RIGHT CLICK to close setting panel.";
	auto h = Label::createWithTTF(hint, FONTA, 24, Size::ZERO, TextHAlignment::RIGHT);
	h->setTextColor(TEXTCOLORB);
	UI::setNodeAnch(h, ANCH_TR);
	h->setPosition(frameSize.width*.95f, frameSize.height*.95f);
	addChild(h);
}

void UISetting::createTab(string name, string iconfile)
{
	auto tab = UICButton::create(Size(frameSize.width*.15f, frameSize.height*.05f), name, THEMECOLORBA(0), TEXTCOLORB);
	tab->setTag(tabcount++);
	tab->setEventFlag(SettingOptionEventFlag);
	UI::setNodeAnch(tab, ANCH_TL);
	tab->setPosition(frameSize.width*-.1f + (frameSize.width*.16f + 10)*tabcount, frameSize.height * .85f);
	tab->additionalEventActions.onMouseDown = CC_CALLBACK_0(UISetting::onTabClick, this);
	addChild(tab);
	tabs.push_back(tab);
}

void UISetting::createPanel(float panelheight, int pagecount, int colcount, int rowcount)
{
	panel = UISettingPanel::create(panelheight, pagecount, colcount, rowcount);
	addChild(panel);
}

void UISetting::onMouseDown(Event * e)
{
	if (mouseStatus == MOUSEHOVER && mouseKey == MOUSERIGHT)
	{
		runAdditionalEventAction(MOUSEDOWN);
		close();
	}
}

UISettingPanel * UISettingPanel::create(float panelheight, int pagecount, int colcount, int rowcount)
{
	auto uisp = new UISettingPanel();
	uisp->initWithColor(TEXTCOLORBA(32), TEXTCOLORBA(0));
	uisp->changeWidthAndHeight(frameSize.width*pagecount, panelheight);
	UI::setNodeAnch(uisp, ANCH_BL);

	uisp->pagecount = pagecount;
	uisp->colcount = colcount;
	uisp->rowcount = rowcount;
	uisp->baseposx = frameSize.width*.05f;
	uisp->panelheight = panelheight;
	uisp->colwidth = frameSize.width*.9f / colcount;
	uisp->rowheight = panelheight / rowcount;
	uisp->uicsize = Size(uisp->colwidth*.618f, uisp->rowheight*.45f);

	uisp->createOptions();

	return uisp;
}

void UISettingPanel::addOption(int page, int col, int row, string name, Node * uicomponent)
{

	float posx = frameSize.width*page + baseposx + colwidth*col;
	float posxm = posx + colwidth*.95f;
	float posy = panelheight - rowheight*(row + .5f);

	auto text = Label::createWithTTF(name, FONTA, rowheight*.3f);
	text->setTextColor(TEXTCOLORB);
	UI::setNodeAnch(text, ANCH_BL);
	text->setPosition(posx, posy);
	addChild(text);

	auto line = LayerColor::create(TEXTCOLORB, colwidth*.95f, 3);
	UI::setNodeAnch(line, ANCH_BL);
	line->setPosition(posx, posy);
	addChild(line);

	UI::setNodeAnch(uicomponent, ANCH_BR);
	uicomponent->setPosition(posxm, posy - rowheight*.5f);
	addChild(uicomponent);
}

void UISettingPanel::createOptions()
{
	SettingOptionSize = uicsize;
	addOption(0, 0, 0, "Resolution", optionResolution());
	addOption(0, 0, 1, "Multi Samples Anti-Aliasing (Recommend 8x) *", optionMultisample());
	addOption(0, 0, 2, "Vertical Synchronization (Recommend Disable)", optionVsync());
	addOption(0, 0, 3, "Frame Rate Limit", optionFpsLimit());
	addOption(0, 1, 0, "Master Volume", optionMasterVolume());
	addOption(0, 1, 1, "Music Volume", optionMusicVolume());
	addOption(0, 1, 2, "Key Sound Volume (unavailable now)", UICSliderBar::create(SettingOptionSize, Color4B(32, 32, 32, 224), Color4B::WHITE, 0, 100, 1, 0, 0));
	addOption(0, 1, 3, "UI Sound Volume (unavailable now)", UICSliderBar::create(SettingOptionSize, Color4B(32, 32, 32, 224), Color4B::WHITE, 0, 100, 1, 0, 0));

	auto keycfgsize = Size(colwidth*.95f, uicsize.height);
	addOption(1, 0, 0, "Normal Style 4 Tracks", UISettingKeybind::create(keycfgsize, 4, 1, THEMECOLORB, TEXTCOLORB));
	addOption(1, 0, 1, "Normal Style 5 Tracks", UISettingKeybind::create(keycfgsize, 5, 1, THEMECOLORB, TEXTCOLORB));
	addOption(1, 0, 2, "Normal Style 6 Tracks", UISettingKeybind::create(keycfgsize, 6, 1, THEMECOLORB, TEXTCOLORB));
	addOption(1, 0, 3, "Normal Style 7 Tracks", UISettingKeybind::create(keycfgsize, 7, 1, THEMECOLORB, TEXTCOLORB));
	addOption(1, 0, 4, "Normal Style 8 Tracks", UISettingKeybind::create(keycfgsize, 8, 1, THEMECOLORB, TEXTCOLORB));
	addOption(1, 1, 0, "BM Style 4 Tracks", UISettingKeybind::create(keycfgsize, 4, 2, THEMECOLORB, TEXTCOLORB));
	addOption(1, 1, 1, "BM Style 5 Tracks", UISettingKeybind::create(keycfgsize, 5, 2, THEMECOLORB, TEXTCOLORB));
	addOption(1, 1, 2, "BM Style 6 Tracks", UISettingKeybind::create(keycfgsize, 6, 2, THEMECOLORB, TEXTCOLORB));
	addOption(1, 1, 3, "BM Style 7 Tracks", UISettingKeybind::create(keycfgsize, 7, 2, THEMECOLORB, TEXTCOLORB));
	addOption(1, 1, 4, "BM Style 8 Tracks", UISettingKeybind::create(keycfgsize, 8, 2, THEMECOLORB, TEXTCOLORB));
}

UISettingKeybind * UISettingKeybind::create(Size size, int keycount, int gamestyle, Color4B bgcolor, Color4B fgcolor)
{
	auto uisk = new UISettingKeybind();
	uisk->initWithColor(bgcolor);
	uisk->changeWidthAndHeight(size.width, size.height);
	uisk->setEventFlag(SettingOptionEventFlag);
	uisk->size = size;
	uisk->keyCount = keycount;
	uisk->fgcolor = fgcolor;
	uisk->bgcolor = bgcolor;

	uisk->keySet = pConfigKeys->getKeyset(keycount, gamestyle);
	uisk->createKeyLabels(uisk->keySet, keycount);

	return uisk;
}

void UISettingKeybind::createKeyLabels(KEYCODE * keyset, int keycount)
{
	auto lsize = Size(size.width / (keycount + 2), size.height*.9f);
	for (int i = 1; i <= keycount; i++)
	{
		auto underline = LayerColor::create(fgcolor, lsize.width, size.height*.1f);
		UI::setNodeAnch(underline, ANCH_MB);
		underline->setPosition(i*size.width / (keycount + 1), 0);
		underline->setTag(i + 10);
		addChild(underline);

		LPSTR keyname = new char[16];
		auto vsc = MapVirtualKey(keyset[i - 1], MAPVK_VK_TO_VSC);
		int res = GetKeyNameTextA((LONG)vsc << 16, keyname, 16);
		auto lasterr = GetLastError();

		auto keytext = Label::createWithTTF(string(keyname), FONTA, size.height*.7f);
		keytext->setTextColor(fgcolor);
		keytext->setVerticalAlignment(TextVAlignment::CENTER);
		keytext->setHorizontalAlignment(TextHAlignment::CENTER);
		keytext->setOverflow(Label::Overflow::SHRINK);
		keytext->setDimensions(lsize.width, size.height*.9f);
		UI::setNodeAnch(keytext, ANCH_MB);
		keytext->setPosition(i*size.width / (keycount + 1), size.height*.1f);
		keytext->setTag(i + 20);
		addChild(keytext);
	}
}

string UISettingKeybind::mapKeycodeToString(KEYCODE keycode)
{
	return string();
}

